#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from rpy   import *
from PyQt4 import QtGui
from PyQt4 import QtCore

x1 = [5.05, 6.75, 3.21, 2.66]
x2 = [2.05, 3.75, 1.21, 8.66]
y =  [1.65, 26.5, -5.93, 7.96]


r.assign('x1',x1)
r.assign('x2',x2)
r.assign('y',y)


print "***** REGRESION LINEAL SIMPLE ******"


SLM = r('linear_model_simple = lm(y~x1)') ##Se debe poner en y y en x1 las variables adecuadas segun lo seleccionado por el usuario en la interfaz

print SLM

bo = SLM['coefficients']['(Intercept)'] #Coeficiente independiente
b1 = SLM['coefficients']['x1'] #Coeficiente de la x en el modelo

print "Modelo obtenido: "
print "Bo = "
print bo
print "B1 = "
print b1

summarySLR = r('SLM_summary = summary(linear_model_simple)')
residualsSLR = summarySLR['residuals']

print "----RESULTADO DEL SUMMARY----"
print "+++Residuales+++"
print residualsSLR['1']
print residualsSLR['2']
print residualsSLR['3']
print residualsSLR['4']

coefficients = summarySLR['coefficients']

print "+++Coeficientes+++"
print "&&&Para bo&&&&"
print "Estimate Std:"
print coefficients[0,0]
print "Error:"
print coefficients[0,1]
print "Valor t:"
print coefficients[0,2]
print "Pr(> |t|): "
print coefficients[0,3]


print "&&&Para b1&&&&"
print "Estimate Std:"
print coefficients[1,0]
print "Error:"
print coefficients[1,1]
print "Valor t:"
print coefficients[1,2]
print "Pr(> |t|): "
print coefficients[1,3]

print "#RCUADRADO#"
R_squared = summarySLR['adj.r.squared']
print R_squared

##GRAFICACION
r.assign('bo_1',bo)
r.assign('b1_1',b1)

r.png("scatter_regression.png", width=400, height=350)
r('plot(x1,y)')
r('abline(bo_1, b1_1, col="red")')

r.dev_off()


print "***** REGRESION LINEAL MULTIPLE DOS VARIABLES ******"


MLM = r('linear_model_multi = lm(y~x1+x2)')


bo = MLM['coefficients']['(Intercept)'] #Coeficiente independiente
b1 = MLM['coefficients']['x1'] #Coeficiente de la x en el modelo
b2 = MLM['coefficients']['x2'] #Coeficiente de la x en el modelo

print "Modelo obtenido: "
print "Bo = "
print bo
print "B1 = "
print b1
print "B2 = "
print b2

summaryMLR = r('MLM_summary = summary(linear_model_multi)')
residualsMLR = summaryMLR['residuals']

print "----RESULTADO DEL SUMMARY----"
print "+++Residuales+++"
print residualsMLR['1']
print residualsMLR['2']
print residualsMLR['3']
print residualsMLR['4']

coefficients = summaryMLR['coefficients']

print "+++Coeficientes+++"
print "&&&Para bo&&&&"
print "Estimate Std:"
print coefficients[0,0]
print "Error:"
print coefficients[0,1]
print "Valor t:"
print coefficients[0,2]
print "Pr(> |t|): "
print coefficients[0,3]


print "&&&Para b1&&&&"
print "Estimate Std:"
print coefficients[1,0]
print "Error:"
print coefficients[1,1]
print "Valor t:"
print coefficients[1,2]
print "Pr(> |t|): "
print coefficients[1,3]

print "&&&Para b2&&&&"
print "Estimate Std:"
print coefficients[2,0]
print "Error:"
print coefficients[2,1]
print "Valor t:"
print coefficients[2,2]
print "Pr(> |t|): "
print coefficients[2,3]

print "#RCUADRADO#"
R_squared = summaryMLR['adj.r.squared']
print R_squared


